<?php
namespace app\index\controller;

use app\index\model\AcckuanModel;
use app\index\model\AccountListModel;
use app\index\model\AccountModel;
use app\index\model\DealbianListModel;
use app\index\model\DealbianModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;
use app\index\model\DeptModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowLogModel;
use app\index\model\OptionFeiModel;
use app\index\relevan\EnginRelevan;
use app\index\service\FinanceService;
use app\index\service\FlowService;
use app\index\service\KuanService;
use app\index\service\MenuService;
use app\index\service\OptService;
use app\index\service\UserService;
use think\Db;
use think\Request;

class Finance extends Common
{
    public function list_option_fei(Request $request){
        $pid = $request->post('pid', 0, 'int');
        if($pid){
            $data['data'] = OptionFeiModel::field('*')
                ->where(['comid'=>$this->userinfo['comid'],'deleted_user'=>0,'pid'=>$pid])
                ->order('sort ASC')->select()->toArray();
        }else{
            $data['data'] = [];
        }

        $data['butauth'] = (new MenuService())->get_butauth('option_fei');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_option_fei_nav(){
        $data = [];
        $nav = ['项目收入','项目支出','其他收入','其他支出','往来款'];
        foreach ($nav as $k=>$v){
            $item['title'] = $v;
            $item['spread'] = true;
            $item['children'] = OptionFeiModel::field('id,name as title')
                ->where(['modname'=>$v])->order('sort ASC')->select()->toArray();
            $data[] = $item;
        }
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_option_fei(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入名称');
            }
            if((new UserService())->check_option_fei_name($param['name'], $param['pid'])){
                return outputJson(4000, '名称已存在');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            unset($param['token']);

            Db::startTrans();
            try {
                $optionid = OptionFeiModel::insertGetId($param);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'option_fei', $optionid, '款项类型');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $pid = $request->get('pid',0,'int');
            $maxsort = OptionFeiModel::where(['comid'=>$this->userinfo['comid'],'pid'=>$pid,'deleted_user'=>0])->max('sort');
            $data['sort'] = $maxsort+1;
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function edit_option_fei(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入名称');
            }

            $pidinfo = OptionFeiModel::where(['id'=>$param['id']])->find();

            if((new UserService())->check_option_name($param['name'], $pidinfo['pid'], $param['id'])){
                return outputJson(4000, '名称已存在');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at']= date('Y-m-d H:i:s');
            unset($param['token']);

            Db::startTrans();
            try {
                OptionFeiModel::update($param);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'option', $param['id'], '款项类型');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data = OptionFeiModel::where(['id'=>$id])->find();
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function move_option_fei(Request $request){
        if($request->isPost()){
            $id = $request->post('id', 0, 'int');
            $type = $request->post('type', 0, 'int');

            $data = OptionFeiModel::where(['id'=>$id])->find();
            if(!$data){
                return outputJson(2000,lang('code_2000'));
            }

            $list = OptionFeiModel::where(['comid'=>$this->userinfo['comid'],'pid'=>$data['pid'],'deleted_user'=>0])
                ->order('sort ASC')->column('id');
            if(count($list) < 2){
                return outputJson(2000,lang('code_2000'));
            }

            $info = [];
            if($type == 1){
                foreach ($list as $k=>$opid){
                    if($opid == $data['id']){
                        if($k == 0){
                            $info[] = $opid;
                        }else{
                            $info[$k-1] = $opid;
                            $info[$k] = $list[$k-1];
                        }
                    }else{
                        $info[] = $opid;
                    }
                }
            }elseif($type == 2){
                foreach ($list as $k=>$opid){
                    if($k > 0){
                        if($list[$k-1] == $data['id']){
                            $info[$k-1] = $opid;
                            $info[$k] = $list[$k-1];
                        }else{
                            $info[] = $opid;
                        }
                    }else{
                        $info[] = $opid;
                    }
                }
            }else{
                $info = $list;
            }

            Db::startTrans();
            try {
                $sort = 1;
                foreach ($info as $_opid){
                    OptionFeiModel::where(['id'=>$_opid])->update(['sort'=>$sort]);
                    $sort++;
                }

                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(2000,lang('code_2000'));
            }
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function del_option_fei(Request $request){
        $param = $request->post();
        $data = OptionFeiModel::where(['id'=>$param['id']])->find();
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        if( $data['isdea'] > 0 ){
            return outputJson(4000, '系统预设数据，不能删除');
        }

        Db::startTrans();
        try {
            OptionFeiModel::where(['id'=>$data['id']])->update([
                'deleted_user'=> $this->userinfo['id'],
                'deleted_at'=> date('Y-m-d H:i:s'),
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'option', $data['id'], '款项类型');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }

    public function list_kuan_shou(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['kuan.deleted_user','=', 0];
        $where[] = ['kuan.comid','=',$this->userinfo['comid']];
        $where[] = ['kuan.modtype','<',20];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kuan.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['kuan.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kuan.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['feilei_name']) && !empty($param['feilei_name'])){
            $where[] = ['feilei.name','like', '%'.$param['feilei_name'].'%'];
        }
        if(isset($param['feitype_name']) && !empty($param['feitype_name'])){
            $where[] = ['feitype.name','like', '%'.$param['feitype_name'].'%'];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';


        $data['modtype'] = (new KuanService())->get_acckuan_modtype(1);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';

        $data['data'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,e.name as enginname,acc.name as accname,acc.accnum,
            bill.sericnum,bill.status,bill.modenum,bill.mid,bill.flowuser,bill.created_at,e.chanid as engin_chanid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->leftJoin('account acc','acc.id=kuan.accid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->leftJoin('option_fei feilei','feilei.id=kuan.feilei')
            ->leftJoin('option_fei feitype','feitype.id=kuan.feitype')
            ->where($where)
            ->order('kuan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                $item['engin_chanid_name'] = DeptModel::get_name_by_pk($item['engin_chanid']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                return $item;
            })->toArray();

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function list_kuan_zhi(Request $request){
        $param = $request->post();
        get_page($param);

        $where = [];
        $whereZhu[] = ['kuan.deleted_user','=', 0];
        $whereZhu[] = ['kuan.comid','=',$this->userinfo['comid']];
        $whereZhu[] = ['kuan.modtype','>',10];

        if(isset($param['enginid']) && !empty($param['enginid'])){
            $where[] = ['kuan.enginid','=', $param['enginid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['kuan.zhai','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['number']) && !empty($param['number'])){
            $where[] = ['kuan.number','like', '%'.$param['number'].'%'];
        }
        if(isset($param['feilei_name']) && !empty($param['feilei_name'])){
            $where[] = ['feilei.name','like', '%'.$param['feilei_name'].'%'];
        }
        if(isset($param['feitype_name']) && !empty($param['feitype_name'])){
            $where[] = ['feitype.name','like', '%'.$param['feitype_name'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['e.chanid','=', $param['chanid']];
        }

        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['modtype'] = (new KuanService())->get_acckuan_modtype(2);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';

        $wherePayss = [];
        if(isset($param['payss']) && !empty($param['payss'])){
            if($param['payss'] == 'no'){
                $wherePayss[] = ['kuan.payfee','=', 0];
                $wherePayss[] = ['kuan.money','neq', 0];
            }
            if($param['payss'] == 'bu'){
                $wherePayss[] = ['kuan.payfee','neq', 0];
                $wherePayss[] = ['kuan.money','neq', 0];
                $wherePayss[] = Db::raw('kuan.payfee < kuan.money');
            }
            if($param['payss'] == 'quan'){
                $wherePayss[] = ['kuan.payfee','neq', 0];
                $wherePayss[] = ['kuan.money','neq', 0];
                $wherePayss[] = Db::raw('kuan.payfee >= kuan.money');
            }
            if($param['payss'] == 'wu'){
                $wherePayss[] = ['kuan.money','=', 0];
            }
        }
        $leftarr = [];
        $leftarr['no'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->leftJoin('option_fei feilei','feilei.id=kuan.feilei')
            ->leftJoin('option_fei feitype','feitype.id=kuan.feitype')
            ->where($where)
            ->where($whereZhu)
            ->where('kuan.payfee','=',0)
            ->where('kuan.money','neq',0)
            ->count();
        $leftarr['bu'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->leftJoin('option_fei feilei','feilei.id=kuan.feilei')
            ->leftJoin('option_fei feitype','feitype.id=kuan.feitype')
            ->where($where)
            ->where($whereZhu)
            ->where('kuan.payfee < kuan.money')
            ->where('kuan.payfee','neq',0)
            ->where('kuan.money','neq',0)
            ->count();
        $leftarr['quan'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->leftJoin('option_fei feilei','feilei.id=kuan.feilei')
            ->leftJoin('option_fei feitype','feitype.id=kuan.feitype')
            ->where($where)
            ->where($whereZhu)
            ->where('kuan.payfee >= kuan.money')
            ->where('kuan.payfee','neq',0)
            ->where('kuan.money','neq',0)
            ->count();
        $leftarr['wu'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->leftJoin('option_fei feilei','feilei.id=kuan.feilei')
            ->leftJoin('option_fei feitype','feitype.id=kuan.feitype')
            ->where($where)
            ->where($whereZhu)
            ->where('kuan.money','=',0)
            ->count();

        $leftarr['all'] = $leftarr['no'] + $leftarr['bu'] + $leftarr['quan'] + $leftarr['wu'];
        $data['leftarr'] = $leftarr;



        $data['data'] = AcckuanModel::alias('kuan')
            ->field('kuan.*,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid,bill.flowuser,bill.created_at,e.chanid as engin_chanid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->leftJoin('option_fei feilei','feilei.id=kuan.feilei')
            ->leftJoin('option_fei feitype','feitype.id=kuan.feitype')
            ->where($where)
            ->where($wherePayss)
            ->where($whereZhu)
            ->order('kuan.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['paystate_name'] = (new KuanService())->get_acckuan_paystate_name($item['paystate']);
                $item['engin_chanid_name'] = DeptModel::get_name_by_pk($item['engin_chanid']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('kuan_zhi');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function del_kuan(Request $request){
        $param = $request->post();
        $data = AcckuanModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AcckuanModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'acckuan', $data['id'], '款项记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }

    public function list_account_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        if(isset($param['type']) && $param['type'] == 1){
            $where[] = ['kuan.modtype','<', 20];
        }
        if(isset($param['type']) && $param['type'] == 2){
            $where[] = ['kuan.modtype','>', 20];
        }

        if(isset($param['accid']) && !empty($param['accid'])){
            $where[] = ['list.accid','=', $param['accid']];
        }
        if(isset($param['kuanid']) && !empty($param['kuanid'])){
            $where[] = ['list.kuanid','=', $param['kuanid']];
        }

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['kuan.duier','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $where[] = ['e.name','like', '%'.$param['enginname'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['feilei']) && !empty($param['feilei'])){
            $where[] = ['kuan.feilei','=', $param['feilei']];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['kuanlei']) && !empty($param['kuanlei'])){
            $where[] = ['kuan.kuanlei','like', '%'.$param['kuanlei'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['list.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }
        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['money']) && !empty($param['money'])){
            $where[] = ['list.money','like', '%'.$param['money'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.name','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }

        if(isset($param['acc_name']) && !empty($param['acc_name'])){
            $where[] = ['acc.name','like', '%'.$param['acc_name'].'%'];
        }
        if(isset($param['acc_accname']) && !empty($param['acc_accname'])){
            $where[] = ['acc.accname','like', '%'.$param['acc_accname'].'%'];
        }
        if(isset($param['acc_accnum']) && !empty($param['acc_accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['acc_accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }

        $data['modtype'] = (new KuanService())->get_acckuan_modtype(0);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';

        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';



        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,e.name as enginname,
            bill.sericnum,bill.status,bill.modenum,bill.mid
            ,kuan.modtype,kuan.enginid,kuan.zhai,kuan.feilei,kuan.feitype,kuan.duier,kuan.incdaotype
            ,acc.name as acc_name,acc.accname as acc_accname,acc.acctype as acctype,acc.accnum as acc_accnum')
            ->join('acckuan kuan','kuan.id=list.kuanid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->leftJoin('account acc','acc.id=list.accid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type_name'] = $item['modtype'] > 20 ? '支出' : '收入';
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['acc_acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['feitype_name'] = OptionFeiModel::where(['id'=>$item['feitype']])->value('name');
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('account_list');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function show_account_list(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountListModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $kuan = AcckuanModel::where(['id'=>$data['data']['kuanid']])->find();
        $kuan['feilei_name'] = OptionFeiModel::where(['id'=>$kuan['feilei']])->value('name');
        $data['kuan'] = $kuan;

        $bill = FlowBillModel::where(['id'=>$kuan['billid']])->find();
        $data['bill'] = $bill;

        $account = AccountModel::get_info_by_pk($data['data']['accid']);
        $data['account'] = $account;

        $data['data']['enginname'] = EnginModel::where(['id'=>$kuan['enginid']])->value('name');
        $data['data']['type_name'] = $kuan['modtype'] > 20 ? '支出' : '收入';
        $data['data']['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($kuan['modtype']);

        $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function detail_account_list(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['list.comid','=',$this->userinfo['comid']];
        $where[] = ['list.kuanid','=',$param['kuanid']];


        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['bill.sericnum|kuan.zhai|kuan.duier|kuan.incdaotype|list.jiaonum|list.baner','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['modtype']) && !empty($param['modtype'])){
            $where[] = ['kuan.modtype','=', $param['modtype']];
        }
        if(isset($param['sericnum']) && !empty($param['sericnum'])){
            $where[] = ['bill.sericnum','like', '%'.$param['sericnum'].'%'];
        }
        if(isset($param['zhai']) && !empty($param['zhai'])){
            $where[] = ['kuan.zhai','like', '%'.$param['zhai'].'%'];
        }
        if(isset($param['duier']) && !empty($param['duier'])){
            $where[] = ['kuan.duier','like', '%'.$param['duier'].'%'];
        }
        if(isset($param['dt']) && !empty($param['dt'])){
            $where[] = ['kuan.dt','between', explode(' - ', $param['dt'])];
        }
        if(isset($param['incdaotype']) && !empty($param['incdaotype'])){
            $where[] = ['kuan.incdaotype','like', '%'.$param['incdaotype'].'%'];
        }
        if(isset($param['jiaonum']) && !empty($param['jiaonum'])){
            $where[] = ['list.jiaonum','like', '%'.$param['jiaonum'].'%'];
        }
        if(isset($param['baner']) && !empty($param['baner'])){
            $where[] = ['list.baner','like', '%'.$param['baner'].'%'];
        }

        $whereEngin = "";
        if(isset($param['enginname']) && !empty($param['enginname'])){
            $whereEngin = "b.name like '%".$param['enginname']."%' or e.name like '%".$param['enginname']."%'";
        }

        $data['modtype'] = (new KuanService())->get_acckuan_modtype(1);
        $data['is_modtype'] = isset($param['modtype']) ? $param['modtype'] : '';


        $data['data'] = AccountListModel::alias('list')
            ->field('list.*,e.name as enginname,acc.name as accname,acc.accnum,
            bill.sericnum,bill.status,bill.modenum,bill.mid,
            kuan.modtype,kuan.enginid,kuan.zhai,kuan.feilei,kuan.duier,kuan.incdaotype')
            ->join('acckuan kuan','kuan.id=list.kuanid')
            ->leftJoin('engin e','e.id=kuan.enginid')
            ->leftJoin('account acc','acc.id=list.accid')
            ->join('flow_bill bill','bill.id=kuan.billid')
            ->where($where)
            ->where($whereEngin)
            ->order('list.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type_name'] = $item['modtype'] > 20 ? '支出' : '收入';
                $item['feilei_name'] = OptionFeiModel::where(['id'=>$item['feilei']])->value('name');
                $item['modtype_name'] = (new KuanService())->get_acckuan_modtype_name($item['modtype']);
                $item['accid_name'] = $item['accname'].'('.$item['accnum'].')';
                return $item;
            })->toArray();
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_account_list(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['money'])){
                return outputJson(4000, '请输入本次支付金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $param['jiaonum'] = (new FinanceService())->create_account_list_jiaonum_zhi();
            $kuan = AcckuanModel::where(['id'=>$param['kuanid']])->find();
            $nofee = round($kuan['money'] - $kuan['payfee'], 2);
            if($param['money'] > $nofee){
                return outputJson(4000, '本次支付金额不能大于未付金额');
            }


            $param['modtype'] = $kuan['modtype'];
            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'account_list') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                $id = AccountListModel::insertGetId($param);

                $xinPayfee = AccountListModel::where(['kuanid'=>$param['kuanid']])->sum('money');
                if($kuan['money'] > 0){
                    if($xinPayfee > 0){
                        if($xinPayfee >= $kuan['money']){
                            $xinPaystate = 2;
                        }else{
                            $xinPaystate = 1;
                        }
                    }else{
                        $xinPaystate = 0;
                    }
                }else{
                    $xinPaystate = 2;
                }

                $paynofee = $kuan['money'] - $xinPayfee;
                $paylastdt = AccountListModel::where(['kuanid'=>$param['kuanid']])->order('dt DESC')->value('dt');
                AcckuanModel::where(['id'=>$param['kuanid']])->update([
                    'paystate'=>$xinPaystate,
                    'paylastdt'=>$paylastdt,
                    'payfee'=>$xinPayfee,
                    'paynofee'=>$paynofee,
                ]);
                (new EnginRelevan())->set_account_list_vateinfo($param['kuanid']);


                $bill = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$kuan['billid']])->find();
                $paystate_name = (new KuanService())->get_acckuan_paystate_name($xinPaystate);
                $mess = "您[".$bill['created_at']."]发起的[".$bill['summary']."]已支付[".$xinPayfee."]元（[".$paystate_name."]）。";
                (new OptService())->add_todo($bill['created_user'], $bill['modename'],$mess, $bill['mid'], $bill['modenum']);


                (new OptService())->relation_file($files,'account_list',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account_list', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account_list', $id, '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $kuanid = $request->get('kuanid',0,'int');
            $kuan = AcckuanModel::where(['id'=>$kuanid])->find();
            if(!$kuan){
                return outputJson(4000, lang('code_4000'));
            }

            $kuan['enginname'] = EnginModel::where(['id'=>$kuan['enginid']])->value('name');
            $kuan['nofee'] = round($kuan['money'] - $kuan['payfee'], 2);
            $kuan['feilei_name'] = OptionFeiModel::where(['id'=>$kuan['feilei']])->value('name');
            $data['kuan'] = $kuan;

            $data['bill'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$kuan['billid']])->find();

            $data['dt'] = date("Y-m-d");
            $data['baner'] = $this->userinfo['realname'];
            $data['jiaonum'] = (new FinanceService())->create_account_list_jiaonum_zhi();
            $data['files'] = (new OptService())->get_file('account_list');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account_list(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['money'])){
                return outputJson(4000, '请输入本次支付金额');
            }
            if(empty($param['accid'])){
                return outputJson(4000, '请选择支付账户');
            }
            if(empty($param['dt'])){
                return outputJson(4000, '请选择交易日期');
            }
            if(empty($param['baner'])){
                return outputJson(4000, '请输入经手人');
            }

            $oldinfo = AccountListModel::where(['id'=>$param['id']])->find();
            $kuan = AcckuanModel::where(['id'=>$oldinfo['kuanid']])->find();
            $kuan['payfee'] = AccountListModel::where(['kuanid'=>$oldinfo['kuanid']])->where('id','neq',$param['id'])->sum('money');
            $nofee = round($kuan['money'] - $kuan['payfee'], 2);
            if($param['money'] > $nofee){
                return outputJson(4000, '本次支付金额不能大于未付金额');
            }


            $param['comid'] = $this->userinfo['comid'];
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'account_list') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                AccountListModel::update($param);

                $xinPayfee = AccountListModel::where(['kuanid'=>$oldinfo['kuanid']])->sum('money');
                if($kuan['money'] > 0){
                    if($xinPayfee > 0){
                        if($xinPayfee >= $kuan['money']){
                            $xinPaystate = 2;
                        }else{
                            $xinPaystate = 1;
                        }
                    }else{
                        $xinPaystate = 0;
                    }
                }else{
                    $xinPaystate = 2;
                }

                $paynofee = $kuan['money'] - $xinPayfee;
                $paylastdt = AccountListModel::where(['kuanid'=>$oldinfo['kuanid']])->order('dt DESC')->value('dt');
                AcckuanModel::where(['id'=>$oldinfo['kuanid']])->update([
                    'paystate'=>$xinPaystate,
                    'paylastdt'=>$paylastdt,
                    'payfee'=>$xinPayfee,
                    'paynofee'=>$paynofee,
                ]);
                (new EnginRelevan())->set_account_list_vateinfo($oldinfo['kuanid']);

                (new OptService())->relation_file($files,'account_list',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account_list', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account_list', $param['id'], '交易记录');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountListModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $kuan = AcckuanModel::where(['id'=>$data['data']['kuanid']])->find();
            $kuan['enginname'] = EnginModel::where(['id'=>$kuan['enginid']])->value('name');
            $kuan['nofee'] = round($kuan['money'] - $kuan['payfee'], 2);
            $kuan['feilei_name'] = OptionFeiModel::where(['id'=>$kuan['feilei']])->value('name');
            $data['kuan'] = $kuan;

            $data['bill'] = FlowBillModel::where(['comid'=>$this->userinfo['comid'],'id'=>$kuan['billid']])->find();

            $account = AccountModel::get_info_by_pk($data['data']['accid']);
            $data['account'] = $account;

            $data['files'] = (new OptService())->get_file('account_list', $data['data']['id']);
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function delete_account_list(Request $request){
        $param = $request->post();
        $data = AccountListModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountListModel::where(['id'=>$data['id']])->delete();

            $kuan = AcckuanModel::where(['id'=>$data['kuanid']])->find();
            $xinPayfee = AccountListModel::where(['kuanid'=>$data['kuanid']])->sum('money');
            if($kuan['money'] > 0){
                if($xinPayfee > 0){
                    if($xinPayfee >= $kuan['money']){
                        $xinPaystate = 2;
                    }else{
                        $xinPaystate = 1;
                    }
                }else{
                    $xinPaystate = 0;
                }
            }else{
                $xinPaystate = 2;
            }


            $paynofee = $kuan['money'] - $xinPayfee;
            $paylastdt = AccountListModel::where(['kuanid'=>$data['kuanid']])->order('dt DESC')->value('dt');
            AcckuanModel::where(['id'=>$data['kuanid']])->update([
                'paystate'=>$xinPaystate,
                'paylastdt'=>$paylastdt,
                'payfee'=>$xinPayfee,
                'paynofee'=>$paynofee,
            ]);
            (new EnginRelevan())->set_account_list_vateinfo($data['kuanid']);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account_list', $data['id'], '交易记录');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }

    public function list_dealcom(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['deal.deleted_user','=', 0];
        $where[] = ['deal.comid','=',$this->userinfo['comid']];
        $where[] = ['deal.isturn','=',1];
        $where[] = ['deal.status','in',[0,1]];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['deal.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['deal.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['type']) && !empty($param['type'])){
            $where[] = ['deal.type','=', $param['type']];
        }
        if(isset($param['nature']) && !empty($param['nature'])){
            $where[] = ['deal.nature','=', $param['nature']];
        }
        if(isset($param['legaler']) && !empty($param['legaler'])){
            $where[] = ['deal.legaler','like', '%'.$param['legaler'].'%'];
        }
        if(isset($param['lianer']) && !empty($param['lianer'])){
            $where[] = ['deal.lianer','like', '%'.$param['lianer'].'%'];
        }
        if(isset($param['liantel']) && !empty($param['liantel'])){
            $where[] = ['deal.liantel','like', '%'.$param['liantel'].'%'];
        }
        if(isset($param['taxtype']) && !empty($param['taxtype'])){
            $where[] = ['deal.taxtype','=', $param['taxtype']];
        }
        if(isset($param['ratenum']) && !empty($param['ratenum'])){
            $where[] = ['deal.ratenum','like', '%'.$param['ratenum'].'%'];
        }
        if(isset($param['status']) && $param['status'] != ''){
            $where[] = ['deal.status','=', $param['status']];
        }

        $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
        $data['is_type'] = isset($param['type']) ? $param['type'] : '';

        $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
        $data['is_taxtype'] = isset($param['taxtype']) ? $param['taxtype'] : '';

        $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
        $data['is_nature'] = isset($param['nature']) ? $param['nature'] : '';

        $data['data'] = DealcomModel::alias('deal')
            ->field('deal.*,bill.sericnum,bill.modename')
            ->leftJoin('flow_bill bill','bill.id=deal.billid')
            ->where($where)
            ->where((new MenuService())->get_miss_where('dealcom', 'deal'))
            ->order('deal.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key){
                $item['type_name'] = (new OptService())->get_base_showname('cai_dealcom_type', $item['type']);
                $item['nature_name'] = (new OptService())->get_base_showname('cai_dealcom_nature', $item['nature']);
                $item['taxtype_name'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $item['taxtype']);
                $item['piaotype_name'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $item['piaotype']);
                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('dealcom');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function edit_dealcom(Request $request){
        if($request->isPost()){
            $param = $request->post();
            if(empty($param['name'])){
                return outputJson(4000, '请输入单位名称');
            }
            if((new FinanceService())->check_dealcom_name($param['name'], $param['id'])){
                return outputJson(4000, '单位名称已存在');
            }
            if(empty($param['type'])){
                return outputJson(4000, '请选择所属分类');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'dealcom') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);


            $list = array_filter($param['list']);
            unset($param['list']);

            if(count($list) > 0){
                $isdea = 0;
                foreach ($list as $vvv){
                    if($vvv['isdea'] > 0){
                        $isdea = 1;
                    }
                }
                if($isdea < 1){
                    return outputJson(4000, '账户信息,请选择默认账户');
                }
            }

            Db::startTrans();
            try {
                DealcomModel::update($param);

                DealcomListModel::where(['dealid'=>$param['id']])->delete();
                foreach($list as $vv){
                    $vv['dealid'] = $param['id'];
                    DealcomListModel::insert($vv);
                }

                (new FinanceService())->set_dealcom_zhanginfo($param['id']);

                (new OptService())->relation_file($files,'dealcom',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'dealcom', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'dealcom', $param['id'], '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }

            $list = DealcomListModel::where(['dealid'=>$data['data']['id']])->select()->toArray();
            $data['list'] = $list;


            $data['type'] = (new OptService())->get_base_info('cai_dealcom_type');
            $data['taxtype'] = (new OptService())->get_base_info('cai_dealcom_taxtype');
            $data['piaotype'] = (new OptService())->get_base_info('cai_dealcom_piaotype');
            $data['nature'] = (new OptService())->get_base_info('cai_dealcom_nature');
            $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_dealcom(Request $request){
        $param = $request->post();
        $data = DealcomModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            FlowBillModel::where(['id'=>$data['billid']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);
            DealcomModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'dealcom', $data['id'], '往来单位');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function add_dealcom_list(Request $request){
        if($request->isPost()){
            $param = $request->post();

            if(empty($param['zhangname'])){
                return outputJson(4000, '请输入账户名');
            }
            if(empty($param['bankname'])){
                return outputJson(4000, '请输入开户行及行号');
            }
            if(empty($param['banknum'])){
                return outputJson(4000, '请输入银行账号');
            }

            unset($param['token']);

            Db::startTrans();
            try {
                if($param['isdea'] == 1){
                    DealcomListModel::where(['dealid'=>$param['dealid']])->update(['isdea'=>0]);
                }
                DealcomListModel::insertGetId($param);

                (new FinanceService())->set_dealcom_zhanginfo($param['dealid']);

                (new OptService())->add_flow_log('新增银行账户信息', 'dealcom', $param['dealid']);
                (new OptService())->add_log_list('新增银行账户信息', 'dealcom', $param['dealid'], '往来单位');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('dealid',0,'int');
            $data['data'] = DealcomModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function show_dealcom(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DealcomModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $list = DealcomListModel::where(['dealid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['isdea_name'] = $item['isdea'] > 0 ? '是' : '否';
        }
        $data['list'] = $list;

        $data['data']['type_name'] = (new OptService())->get_base_showname('cai_dealcom_type', $data['data']['type']);
        $data['data']['piaotype_name'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $data['data']['piaotype']);
        $data['data']['nature_name'] = (new OptService())->get_base_showname('cai_dealcom_nature', $data['data']['nature']);
        $data['data']['taxtype_name'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $data['data']['taxtype']);
        $data['files'] = (new OptService())->get_file('dealcom', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }
    public function show_dealbian(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = DealbianModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }

        $data['midinfo'] = DealcomModel::where(['id'=>$data['data']['mid']])->find();

        $list = DealbianListModel::where(['dealid'=>$data['data']['id']])->select()->toArray();
        foreach ($list as &$item){
            $item['isdea_name'] = $item['isdea'] > 0 ? '是' : '否';
        }
        $data['list'] = $list;

        $data['data']['type_name'] = (new OptService())->get_base_showname('cai_dealcom_type', $data['data']['type']);
        $data['data']['piaotype_name'] = (new OptService())->get_base_showname('cai_dealcom_piaotype', $data['data']['piaotype']);
        $data['data']['nature_name'] = (new OptService())->get_base_showname('cai_dealcom_nature', $data['data']['nature']);
        $data['data']['taxtype_name'] = (new OptService())->get_base_showname('cai_dealcom_taxtype', $data['data']['taxtype']);
        $data['files'] = (new OptService())->get_file('dealbian', $data['data']['id']);
        $data['billinfo'] = (new FlowService())->get_flow_show_info($data['data']['billid']);
        return outputJson(2000,lang('code_2000'), $data);
    }

    public function list_account(Request $request){
        $param = $request->post();
        get_page($param);

        $where[] = ['acc.deleted_user','=', 0];
        $where[] = ['acc.comid','=',$this->userinfo['comid']];

        if(isset($param['keyword']) && !empty($param['keyword'])){
            $where[] = ['acc.name','like', '%'.$param['keyword'].'%'];
        }
        if(isset($param['name']) && !empty($param['name'])){
            $where[] = ['acc.name','like', '%'.$param['name'].'%'];
        }
        if(isset($param['accname']) && !empty($param['accname'])){
            $where[] = ['acc.accname','like', '%'.$param['accname'].'%'];
        }
        if(isset($param['accnum']) && !empty($param['accnum'])){
            $where[] = ['acc.accnum','like', '%'.$param['accnum'].'%'];
        }
        if(isset($param['acctype']) && !empty($param['acctype'])){
            $where[] = ['acc.acctype','=', $param['acctype']];
        }
        if(isset($param['accmoney']) && !empty($param['accmoney'])){
            $where[] = ['acc.accmoney','like', '%'.$param['accmoney'].'%'];
        }
        if(isset($param['accdt']) && !empty($param['accdt'])){
            $where[] = ['acc.accdt','between', explode(' - ', $param['accdt'])];
        }
        if(isset($param['chanid']) && !empty($param['chanid'])){
            $where[] = ['acc.chanid','=', $param['chanid']];
        }
        $data['chanid'] = (new UserService())->get_chaninfo_all();;
        $data['is_chanid'] = isset($param['chanid']) ? $param['chanid'] : '';

        $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
        $data['is_acctype'] = isset($param['acctype']) ? $param['acctype'] : '';



        $whereJiao = [];
        if(isset($param['dt']) && !empty($param['dt'])){
            $whereJiao[] = ['dt','between', explode(' - ', $param['dt'])];
        }

        $data['data'] = AccountModel::alias('acc')
            ->field('acc.*')
            ->where($where)
            ->where((new MenuService())->get_miss_where('account', 'acc'))
            ->order('acc.id DESC')
            ->paginate($param['pagesize'],false,['page'=>$param['page']])
            ->each(function ($item,$key)use($whereJiao){
                $item['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $item['acctype']);
                $item['chanid_name'] = DeptModel::where(['id'=>$item['chanid']])->value('name');

                $item['shou'] = AccountListModel::where(['accid'=>$item['id']])->where($whereJiao)->where('modtype','<',20)->sum('money');
                $item['zhi'] = AccountListModel::where(['accid'=>$item['id']])->where($whereJiao)->where('modtype','>',20)->sum('money');
                $item['cha'] = round($item['shou'] - $item['zhi'], 2);
                $item['balance'] = round($item['accmoney'] + $item['shou'] - $item['zhi'], 2);

                return $item;
            })->toArray();

        $data['butauth'] = (new MenuService())->get_butauth('account');
        return outputJson(2000,lang('code_2000'),$data);
    }
    public function add_account(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $param['name'] = trim($param['name']);
            if(empty($param['name'])){
                return outputJson(4000, '请输入账户名称');
            }
            if(empty($param['accname'])){
                return outputJson(4000, '请输入开户行');
            }
            if(empty($param['accnum'])){
                return outputJson(4000, '请输入账号');
            }
            if(empty($param['acctype'])){
                return outputJson(4000, '请选择账户类型');
            }
            if($param['accmoney'] == ''){
                return outputJson(4000, '请输入期初金额');
            }
            if(empty($param['accdt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['comid'] = $this->userinfo['comid'];
            $param['status'] = 1;
            $param['created_user'] = $param['updated_user'] = $this->userinfo['id'];
            $param['created_at'] = $param['updated_at']= date('Y-m-d H:i:s');
            $param['deptid'] = $this->userinfo['deptid'];

            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'account') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);


            Db::startTrans();
            try {
                $id = AccountModel::insertGetId($param);
                (new OptService())->relation_file($files,'account',$id);
                (new OptService())->add_flow_log(FlowLogModel::SET_ADD, 'account', $id);
                (new OptService())->add_log_list(FlowLogModel::SET_ADD, 'account', $id, '资金账户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,$e->getMessage());
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
            $data['files'] = (new OptService())->get_file('account');
            return outputJson(2000,lang('code_2000'), $data);
        }
    }
    public function edit_account(Request $request){
        if($request->isPost()){
            $param = $request->post();
            $param['name'] = trim($param['name']);
            if(empty($param['name'])){
                return outputJson(4000, '请输入账户名称');
            }
            if(empty($param['accname'])){
                return outputJson(4000, '请输入开户行');
            }
            if(empty($param['accnum'])){
                return outputJson(4000, '请输入账号');
            }
            if(empty($param['acctype'])){
                return outputJson(4000, '请选择账户类型');
            }
            if($param['accmoney'] == ''){
                return outputJson(4000, '请输入期初金额');
            }
            if(empty($param['accdt'])){
                return outputJson(4000, '请选择期初日期');
            }

            $param['updated_user'] = $this->userinfo['id'];
            $param['updated_at'] = date('Y-m-d H:i:s');
            $files = array_filter($param['files']);
            unset($param['files']);
            if( $rcfmsg = (new OptService())->rela_check_file($files, 'account') ){ return outputJson(4000, $rcfmsg); }
            unset($param['token']);

            Db::startTrans();
            try {
                AccountModel::update($param);
                (new OptService())->relation_file($files,'account',$param['id']);
                (new OptService())->add_flow_log(FlowLogModel::SET_EDIT, 'account', $param['id']);
                (new OptService())->add_log_list(FlowLogModel::SET_EDIT, 'account', $param['id'], '资金账户');
                Db::commit();
            } catch (\Exception $e) {
                Db::rollback();
                return outputJson(4000,lang('code_4000'));
            }
            return outputJson(2000,lang('code_2000'));
        }else{
            $id = $request->get('id',0,'int');
            $data['data'] = AccountModel::where(['id'=>$id])->find();
            if(!$data['data']){
                return outputJson(4000, lang('code_4000'));
            }
            $data['data']['chanid_info'] = (new UserService())->get_users_part_arr($data['data']['chanid'], 'dept');
            $data['acctype'] = (new OptService())->get_base_info('cai_account_acctype');
            $data['files'] = (new OptService())->get_file('account', $data['data']['id']);
            return outputJson(2000,lang('code_2000'),$data);
        }
    }
    public function del_account(Request $request){
        $param = $request->post();
        $data = AccountModel::get_info_by_pk($param['id']);
        if(!$data){
            return outputJson(4000,lang('code_4000'));
        }

        Db::startTrans();
        try {
            AccountModel::where(['id'=>$data['id']])->update([
                'deleted_user' => $this->userinfo['id'],
                'deleted_at' => date('Y-m-d H:i:s')
            ]);

            (new OptService())->add_log_list(FlowLogModel::SET_DELETE, 'account', $data['id'], '资金账户');
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return outputJson(4000,lang('code_4000'));
        }
        return outputJson(2000,lang('code_2000'));
    }
    public function show_account(Request $request){
        $id = $request->post('id', 0, 'int');
        $data['data'] = AccountModel::get_info_by_pk($id);
        if(!$data['data']){
            return outputJson(4000, lang('code_4000'));
        }
        $data['data']['status_name'] = $data['data']['status'] > 0 ? '正常' : '停用';
        $data['data']['chanid_name'] = DeptModel::where(['id'=>$data['data']['chanid']])->value('name');
        $data['data']['acctype'] = (new OptService())->get_base_showname('cai_account_acctype', $data['data']['acctype']);
        $data['files'] = (new OptService())->get_file('account', $data['data']['id']);
        return outputJson(2000,lang('code_2000'), $data);
    }











}
